<template>
	<view class="box">
		<view class="box-tips">
			<h2 class='h2'>
				{{verificationTitle.title}}
			</h2>
			<view class="verification">{{verificationTitle.desc}}</view>
		</view>
		<view class="form">
			<u-form :model="form" ref="uForm">

					<u-form-item label-width="120" label="密码" prop="password">
						<u-input type="password" v-model="form.password" placeholder="请输入您的密码" />
					</u-form-item>

					<u-form-item label-width="120" label="确认密码" prop="confirmPwd">
						<u-input type="password" v-model="form.confirmPwd" placeholder="请再次输入您的密码" />
					</u-form-item>

					<view class="submit" @click="setPassword">设置密码</view>
			</u-form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				verificationTitle: {
					title: "设置密码",
					desc: "请设置您的密码",
				},
				form: {
          password: "", //手机号
          confirmPwd: "", //验证码
				},
				// 验证码登录校验
				codeRules: {
          password: [
            {
              required: true,
              message: '请输入密码',
              trigger: ['blur', 'change'],
            },
            {
              pattern: /^[0-9a-zA-Z]{6,18}$/g,
              // 正则检验前先将值转为字符串
              transform(value) {
                return String(value);
              },
              message: '只能包含6-18位数字或字符组合'
            }],
          confirmPwd: [
            {
              required: true,
              message: '请输入确认密码',
              trigger: ['blur', 'change'],
            },
            {
              pattern: /^[0-9a-zA-Z]{6,18}$/g,
              // 正则检验前先将值转为字符串
              transform(value) {
                return String(value);
              },
              message: '只能包含6-18位数字或字符组合'
            }],
				},
			};
		},
		onReady() {
			// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
			this.$refs.uForm.setRules(this.codeRules);
		},

		methods: {
			// 设置密码
      setPassword() {
        this.$refs.uForm.validate(valid =>{
          if (valid){
            if(this.form.password !== this.form.confirmPwd){
              uni.showToast({
                title: "两次输入密码不一致!",
                icon: "none",
              });
              return;
            }
            let param = {
              data :  this.form
            }
            this.$clApi.login.setPwd(param).then(res => {
              uni.showToast({
                title: "设置成功!",
                duration: 2000,
                icon: "none",
              });
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1,
                });
              }, 1000);
            })
          }else{
            console.log('验证失败');
          }
        })

      }
		},
	};
</script>
<style lang="scss" scoped>
	@import url("@/pages/passport/login.scss");

	/deep/ .u-form-item {
		margin: 40rpx 0;
	}

	.sendCode {
		/deep/ .u-form-item--right__content__slot {
			display: flex;
		}
	}

	.h2 {
		font-size: 40rpx;
		font-weight: bold;
	}

	page {
		background: #fff;
	}

	.box {
		padding: 80rpx 0;
		border-radius: 20rpx;
	}

	.submit {
		background: $light-color;
	}

	.box-tips {
		margin: 0 72rpx;
	}

	.verification {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}
</style>
